package com.gxgeek.eshopinventory.redis;

import com.gxgeek.eshopinventory.utils.JedisUtils;
import org.springframework.stereotype.Repository;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisCommands;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;

/**
 * @author gaoxing
 * @create 2018-01-12 下午5:22
 */
@Repository
public class RedisDAO {

    @Resource
    private JedisCluster jedisCluster;
    @Resource
    private JedisPool jedisPool;

    public void set(String key, String value) {
        JedisCommands jedisCommands = JedisUtils.getJedisCommands(jedisCluster, jedisPool);
        try {
            jedisCommands.set(key, value);
        }finally {
            JedisUtils.releaseJedisConnection(jedisCommands);
        }
    }

    public String get(String key) {
        JedisCommands jedisCommands = JedisUtils.getJedisCommands(jedisCluster, jedisPool);
        try {
            return  jedisCommands.get(key);
        }finally {
            JedisUtils.releaseJedisConnection(jedisCommands);
        }
    }
    public Long delete(String key) {
        JedisCommands jedisCommands = JedisUtils.getJedisCommands(jedisCluster, jedisPool);
        try {
            return  jedisCommands.del(key);
        }finally {
            JedisUtils.releaseJedisConnection(jedisCommands);
        }
    }

}
